﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    public class PdfHelper
    {
        /// <summary>
        /// 生成pdf文件
        /// </summary>
        /// <param name="path">文件保存路径</param>
        /// <param name="dtSource">数据源</param>
        /// <param name="HorV">页面横竖（为空表示竖，有非空值为横）</param>
        public static void ExportPDF(string localFilePath, string  value, string HorV)
        {
            iTextSharp.text.io.StreamUtil.AddToResourceSearch("iTextAsian.dll");
            iTextSharp.text.io.StreamUtil.AddToResourceSearch("iTextAsianCmaps.dll");

            BaseFont bf;
            string basepath = localFilePath;
            try
            {
                bf = BaseFont.CreateFont(basepath + "\\FONTS\\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            }
            catch
            {

                bf = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            }
            iTextSharp.text.Font font = new iTextSharp.text.Font(bf);

            iTextSharp.text.Document pdf;
            if (string.IsNullOrEmpty(HorV))
                pdf = new iTextSharp.text.Document();
            else
                pdf = new iTextSharp.text.Document(PageSize.A4.Rotate());

            
            using (FileStream fs = new FileStream(localFilePath, FileMode.Create, FileAccess.Write))
            {
                
                PdfWriter.GetInstance(pdf, fs);
                pdf.Open();
                pdf.Add(new Paragraph(value, font));
                pdf.Close();
            }
        } 
    }
}